package com.zuxia.handler;

import com.zuxia.exception.ZuxiaBusinessException;
import com.zuxia.utils.R;
import com.zuxia.utils.ResultCodeEnum;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import static org.reflections.Reflections.log;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        log.error(e.getMessage());
        return R.error();
    }

    @ExceptionHandler(ZuxiaBusinessException.class)
    @ResponseBody
    public R error(ZuxiaBusinessException e){
        return R.error().code(e.getCode()).message(e.getMessage());
    }

    @ExceptionHandler(BadSqlGrammarException.class)
    @ResponseBody
    public R error(BadSqlGrammarException e){
        return R.setResult(ResultCodeEnum.BAD_SQL_GRAMMAR);
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public R error(HttpMessageNotReadableException e){
        return R.setResult(ResultCodeEnum.JSON_PARSE_ERROR);
    }

}